import re
#原义字符串
s= 'c:\\d\\c'
print(s)
#反斜杠为转义字符
s = '\\n123'
print(s)
#使用原生字符串r
s =r'\\n123'
print(s)
#在正则表达式中反斜杠表示转义
s = '\n123'
pattern = '\\n\d{3}'
# print(re.match(pattern,s))
s = '\\n123'
pattern = '\\\\n\d{3}'
# print('\\\\n\d{3}:',re.match(pattern,s))
s = '\\n123'
pattern = r'\\n\d{3}'
print('\\\\n\d{3}:',re.match(pattern,s))
#使用边界字符
#匹配字符串开头^
pattern = '^hello.*'
# s = 'hello world'
s = 'hell world'#如果不是以hello开头返回是None
print(re.match(pattern,s))
#匹配字符串结尾$
#匹配{5,10}位结尾的qq邮箱
pattern = '[0-9]\d{4,9}@qq.com$'
s = '123456@qq.com'
print(re.match(pattern,s))
#匹配一个单词的边界\b
pattern = r'.*\bqwe'#左边界
s = '123 qwe'#需要空格或者逗号
print(re.match(pattern,s))
pattern = r'.*ing\b'#右边界
s = 'rfging'
print(re.match(pattern,s))
#匹配非单词的边界\B  与\b相反
pattern = r'.*\Bqwe'#左边界
s = '123 qwe'#返回None
S = '123qwe'
print(re.match(pattern,S))
pattern = r'.*ing\B'#右边界
s = 'rfginga'
s = 'rfging'
print(re.match(pattern,s))
#search函数
pattern = 'hello'
s = 'hello world'
result = re.match(pattern,s)
result1 = re.search(pattern,s)
print('search方法的使用：',result1.group())#march和search共同点，获取匹配结果用group
print('match方法的使用：',result.group())
pattern = 'hello'
s = 'Hello world hello'
result = re.match(pattern,s)
result1 = re.search(pattern,s)
print('search方法的使用：',result1.group())#可以匹配出结果
# print('match方法的使用：',result.group())#报错因为没有对象
#择一匹配符|
pattern = 'aa|bb|cc'
print(re.match(pattern,'bb'))
print(re.search(pattern,'bbww'))
#匹配0-100的数字
pattern = r'[1-9]?\d$|100$'#需要注意以两位或者三位数结尾$
# s = '1001'#如果正则表达式不加$返回结果是100.不是None
s = '100'
print(re.match(pattern,s))
#'x|y|z'和'[xyz]'的共同点
pattern = 'x|y|z'
pattern1 = '[xyz]'
s = 'y'
print(re.match(pattern,s))
print(re.match(pattern1,s))
#'x|y|z'和'[xyz]'的不同点
pattern = 'xy|ab'
s = 'ab'
# s1 = 'xa'#返回None
print(re.match(pattern,s))
pattern1 = '[xy][ab]'
s1 = 'xaef'#返回xa
print(re.match(pattern1,s1))




